package com.getpebble.android.main.sections.appstore.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Analytics.MobileAppBehavior;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.framework.fragment.PblBaseFragment;
import com.getpebble.android.jskit.bridge.JsAppLifecycleListener;
import com.getpebble.android.util.HttpUtils;
import java.util.UUID;

public class NoConnectivityFragment extends PblBaseFragment {
    private static final String TAG = NoConnectivityFragment.class.getSimpleName();
    private LinearLayout mRefreshLayout;
    private ImageView mRefreshSpinner;
    private RotateAnimation mRotateAnimation;
    private Context mWebAppContext;
    private UUID mWebAppUUID;

    public void init(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        MobileAppBehavior.logScreenOpened("NoNetworkConnection");
        this.mRefreshSpinner = (ImageView) container.findViewById(R.id.refreshImage);
        this.mRefreshLayout = (LinearLayout) container.findViewById(R.id.refreshLayout);
        this.mRotateAnimation = new RotateAnimation(0.0f, 360.0f, 1, 0.5f, 1, 0.5f);
        this.mRotateAnimation.setDuration(2000);
        this.mRefreshLayout.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                NoConnectivityFragment.this.mRefreshSpinner.startAnimation(NoConnectivityFragment.this.mRotateAnimation);
                Activity activity = NoConnectivityFragment.this.getActivity();
                if (activity == null) {
                    Trace.error(NoConnectivityFragment.TAG, "NoConnectivityFragment: Activity was null.");
                } else if (!HttpUtils.hasInternetConnection(activity.getApplicationContext())) {
                } else {
                    if (NoConnectivityFragment.this.mWebAppContext == null) {
                        activity.getFragmentManager().popBackStack();
                    } else if (NoConnectivityFragment.this.mWebAppContext != null && NoConnectivityFragment.this.mWebAppUUID != null) {
                        activity.getFragmentManager().popBackStack();
                        NoConnectivityFragment.this.startJsWebApp(NoConnectivityFragment.this.mWebAppContext, NoConnectivityFragment.this.mWebAppUUID);
                    }
                }
            }
        });
    }

    public void configureRefreshToStartWebApp(Context context, UUID uuid) {
        this.mWebAppContext = context;
        this.mWebAppUUID = uuid;
    }

    private void startJsWebApp(Context context, UUID uuid) {
        JsAppLifecycleListener.onConfigLaunchRequest(context, uuid);
    }

    public int getLayoutId() {
        return R.layout.fragment_internet_connection_error;
    }
}
